import pandas as pd
import matplotlib.pyplot as plt
import pyecharts.options as opts
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
class Bookbar:
    def __init__(self):
        # 1：数据采集
        #书籍名称	借阅人姓名	班级	借阅日期	借阅天数
        bookrecords = pd.read_csv("data/书籍借阅记录.csv", encoding='utf-8-sig')
        bookcount = bookrecords['借阅天数'].groupby(bookrecords['书籍名称']).sum()
        print(bookcount)
        # 3:数据可视化
        color_function = """
                function (params) {
                    if (params.value > 0 && params.value < 50) {
                        return 'red';
                    } else if (params.value > 50 && params.value < 100) {
                        return 'blue';
                    }
                    return 'green';
                }
  
              """
        x_data = list(bookcount.keys())
        y_data = list(bookcount)
        c = (
            Bar(         init_opts=opts.InitOpts(bg_color='rgba(255,250,205,0.2)',
                                width='100%',
                                height='100%',
                                )
)
                .add_xaxis(x_data)
                .add_yaxis(
                "",
                y_data,
                itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function)),
            )
                .set_global_opts(title_opts=opts.TitleOpts(title=""))
                .render("templates/bar_custom_bar_color1.html")
        )
Bookbar()
